/*
* @Author: dengjiayao
* @Date:   2017-04-19 13:25:50
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-05-03 10:17:22
*/
import Pages from '../../component/pages/pages.vue'
import { showMsg, getAIQuestionList, updateAIQuestion } from '../../vuex/action'

export default {
  name: 'ai-question-list',
  components: {
    'vue-pages': Pages
  },
  vuex: {
    getters: {
      fresh: state => state.AIQuestionListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getAIQuestionList,
      xUpdate: updateAIQuestion
    }
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      search: {
        keyword: '',
        questionType: -1
      }
    }
  },
  ready() {
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      var params = {
        start: start,
        limit: limit
      }
      if (this.search.questionType > -1) {
        params.questionType = +this.search.questionType
      }
      if (this.search.keyword.length) {
        params.questionKeyword = this.search.keyword
      }
      this.xGetList(params)
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    searchList() {
      this.getList({
        start: 0
      })
    },
    updateItem(item) {
      var modal = this.$root.$refs.questionConfig
      modal.actionType = 'UPDATE'
      modal.form = {
        id: item.id,
        questionType: item.questionType,
        question: item.question,
        answerType: item.answerType,
        answerId: item.answerId,
        answerContent: item.answerContent
      }
      modal.showModal = true
    },
    deleteItem(id) {
      this.xUpdate({
        id: id,
        status: 0
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh: function(v) {
      if (v) {
        this.getList({})
      }
    }
  }
}
